package demo1;

import java.util.Scanner;

class MyArray<T> {
    public Object[] array = new Object[10];

    public void set(int pos, T val) {
        array[pos] = val;
    }

    public T get(int pos) {
        return (T)array[pos];//注意，要强制类型转换为我们要的类型T
    }

}


public class Test {

    //装箱 --- 把基本类型转换为包装类型
    public static void main1(String[] args) {
        int a = 10086;
        Integer i = a;
        System.out.println(i);//自动装箱 --- 底层也是调用valueOf

        Integer i2 = Integer.valueOf(a);//手动装箱
        System.out.println(i2);

    }


    //拆箱 --- 把包装类型转换为基本类型
    public static void main2(String[] args) {
        Integer i1 = Integer.valueOf(10);

        int a = i1.intValue(); // 拆箱
        System.out.println(a);
    }






    public static void main(String[] args) {

        MyArray<Integer> myArray1 = new MyArray<Integer>();

        myArray1.set(0,1);
        myArray1.set(1,2);
        myArray1.set(2,3);
        myArray1.set(3,4);

        for(int i = 0; i < 4; i++) {
            System.out.print(myArray1.get(i) + " ");
        }
        System.out.println();


        MyArray<String> myArray2 = new MyArray<String>();

        myArray2.set(0,"hello");
        myArray2.set(1, "World");

        for(int i = 0; i < 2; i++) {
            System.out.print(myArray2.get(i) + " ");
        }
        System.out.println();

    }
}
